using System.Windows.Input;
using DimensionsHelper.Client.ViewModels.DataCollector;

namespace DimensionsHelper.Client.Views.DataCollector;

public partial class IndexFileListPage
{
    public IndexFileListPage(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        DataContext = serviceProvider.GetService<IndexFileListViewModel>();
        IndexListView.PreviewKeyDown += IndexListView_PreviewKeyDown;
    }


    private void RemoveSelectedItems()
    {
        if (IndexListView.SelectedItems is null)
        {
            return;
        }

        foreach (IndexFileViewModel? item in IndexListView.SelectedItems.Cast<IndexFileViewModel>().ToArray())
        {
            ((IndexFileListViewModel)DataContext).Remove(item);
        }
    }


    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        RemoveSelectedItems();
    }


    private void IndexListView_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Delete)
        {
            RemoveSelectedItems();
        }
    }
}